package exp1dtree.record;

import java.util.Map;
import java.util.HashMap;

public final class Record {
	
	public static final int attCnt = 40;
	public static int[] disCnt;
	private static Map<String, Integer> map;
	
	private int[] attributes;
	private boolean positive;
	
	public static void init() {
		map = new HashMap<String, Integer>();
		disCnt = new int[attCnt];
		for (int i = 0; i < attCnt; ++i) {
			map.put(i + "?", 0);
			disCnt[i] = 1;
		}
	}
	
	private static int str2Int(int attID, String s) {
		switch (attID) {
		case 0:		// age
			return 0;
		case 5:		// wage per hour
			return 0;
		case 16:	// capital gains
			return 0;
		case 17:	// capital losses
			return 0;
		case 18:	// dividends from stocks
			return 0;
		case 38:	// weeks worked in year
			return 0;
		default:	// nominal
			s = attID + s;
			if (!map.containsKey(s)) {
				map.put(s, disCnt[attID]++);
			}
			return map.get(s);
		}
	}

	public Record() {
		attributes = null;
		positive = false;
	}
	
	public Record(String s) {
		String[] arrStr = s.split(", ");
		positive = arrStr[attCnt + 1].equals("50000+.");
		int j = 0;
		attributes = new int[attCnt];
		for (int i = 0; i <= attCnt; ++i) {
			if (i == 24)	// instance weight, dropped
				continue;
			attributes[j] = str2Int(j, arrStr[i]);
			++j;
		}
	}
	
	public boolean isPositive() {
		return positive;
	}
	
	public int getAtt(int attID) {
		return attributes[attID];
	}
	
	public boolean lessThan(Record b, int attID) {
		return this.attributes[attID] < b.attributes[attID];
	}
	
	public boolean isSameOutput(Record b) {
		return this.positive == b.positive;
	}
	
	public boolean isSameInput(Record b) {
		for (int i = 0; i < attCnt; ++i)
			if (this.attributes[i] != b.attributes[i])
				return false;
		return true;
	}
	
	public String toString() {
		String s = String.valueOf(positive);
		for (int i : attributes)
			s = s + " ~ " + i;
		return s;
	}
	
}
